package com.du.annulrview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;

/**
 * Created by DuZiLin on 2018/5/3.
 */

public class AnnulrView extends View {
    private RectF rectF;
    private Paint mAnnulrPaint;
    private Paint mAnnulrPaint1;
    private Paint mAnnulrTextPaint;
    private Paint mCenterTextPaint;
    private float textMeasure;
    private float mProgress = 0;
    private Context mContext;
    private ArrayList<ProgressBean> progressBeanList;
    private float[] progresses = new float[5];
    private int[] colors = new int[5];
    private int viewSize;
    private int annulrWidth = 20;
    private int annulrColor = Color.GRAY;
    private int progressTextSize = 55;
    private int progressTextColor = Color.GRAY;
    private float annulrTextSize;
    private int annulrTextColor;
    private boolean canDrawText = false;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            AnnulrView.this.postInvalidate();
        }
    };

    public AnnulrView(Context context) {
        super(context);
        mContext = context;
        init(null);
    }


    public AnnulrView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init(attrs);
    }

    public AnnulrView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        progressBeanList = new ArrayList<>();

        if (attrs != null) {
            initAttr(attrs);
        }

        mAnnulrPaint = new Paint();
        mAnnulrPaint.setColor(annulrColor);
        mAnnulrPaint.setAntiAlias(true);
        mAnnulrPaint.setStyle(Paint.Style.STROKE);
        mAnnulrPaint.setStrokeWidth(annulrWidth);

        mAnnulrPaint1 = new Paint();
        mAnnulrPaint1.setColor(Color.BLUE);
        mAnnulrPaint1.setAntiAlias(true);
        mAnnulrPaint1.setStyle(Paint.Style.STROKE);
        mAnnulrPaint1.setStrokeWidth(annulrWidth);


        mAnnulrTextPaint = new Paint();
        mAnnulrTextPaint.setColor(annulrTextColor);
        mAnnulrTextPaint.setAntiAlias(true);
        mAnnulrTextPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mAnnulrTextPaint.setStrokeWidth(annulrTextSize/15);
        mAnnulrTextPaint.setTextSize(annulrTextSize);
        mAnnulrTextPaint.setTextAlign(Paint.Align.CENTER);

        mCenterTextPaint = new Paint();
        mCenterTextPaint.setColor(progressTextColor);
        mCenterTextPaint.setAntiAlias(true);
        mCenterTextPaint.setStyle(Paint.Style.FILL);
        mCenterTextPaint.setTextSize(progressTextSize);



    }

    private void initAttr(AttributeSet attrs) {
        TypedArray typedValue = mContext.obtainStyledAttributes(attrs, R.styleable.AnnulrView);
        annulrWidth = (int) typedValue.getDimension(R.styleable.AnnulrView_annulr_width, 20);
        annulrColor = typedValue.getColor(R.styleable.AnnulrView_annulr_color, Color.GRAY);
        progressTextSize = (int) typedValue.getDimension(R.styleable.AnnulrView_center_textsize, 55);
        progressTextColor = typedValue.getColor(R.styleable.AnnulrView_center_textcolor, Color.GRAY);
        annulrTextSize = (int) typedValue.getDimension(R.styleable.AnnulrView_annulr_textsize , 60);
        annulrTextColor = typedValue.getColor(R.styleable.AnnulrView_annulr_textcolor , Color.BLACK);
        typedValue.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        switch (mode) {
            case MeasureSpec.AT_MOST:
                viewSize = 500;
                break;
            case MeasureSpec.EXACTLY:
            case MeasureSpec.UNSPECIFIED:
                viewSize = MeasureSpec.getSize(widthMeasureSpec) > MeasureSpec.getSize(heightMeasureSpec) ? MeasureSpec.getSize(heightMeasureSpec) : MeasureSpec.getSize(widthMeasureSpec);
                break;
        }
        setMeasuredDimension(viewSize, viewSize);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        RectF rectF = new RectF(annulrWidth / 2, annulrWidth / 2, viewSize - annulrWidth / 2, viewSize - annulrWidth / 2);
//        RectF rectF1 = new RectF(annulrWidth * (3f / 2), annulrWidth * (3f / 2), viewSize - annulrWidth * (3f / 2), viewSize - annulrWidth * (3f / 2));
//        RectF rectF2 = new RectF(annulrWidth * (5f / 2), annulrWidth * (5f / 2), viewSize - annulrWidth * (5f / 2), viewSize - annulrWidth * (5f / 2));
//        RectF rectF3 = new RectF(annulrWidth * (3f / 2), annulrWidth * (3f / 2), viewSize - annulrWidth * (3f / 2), viewSize - annulrWidth * (3f / 2));
//        RectF rectF4 = new RectF(annulrWidth * (3f / 2), annulrWidth * (3f / 2), viewSize - annulrWidth * (3f / 2), viewSize - annulrWidth * (3f / 2));
//        canvas.drawArc(rectF, -90, progresses[0] / 100 * 360, false, mAnnulrPaint);
//        canvas.drawArc(rectF1, -90, progresses[1] / 100 * 360, false, mAnnulrPaint1);
//        canvas.drawArc(rectF2, -90, progresses[2] / 100 * 360, false, mAnnulrTextPaint);

        for (float i = 0; i < progressBeanList.size(); i++) {
            mAnnulrPaint.setColor(colors[(int) i]);
            rectF = new RectF(annulrWidth * ( (i*2+1) / 2), annulrWidth * ( (i*2+1) / 2), viewSize - annulrWidth * ( (i*2+1) / 2), viewSize - annulrWidth * ( (i*2+1) / 2));
            canvas.drawArc(rectF, -90, progresses[(int) i] / 100 * 360, false, mAnnulrPaint);
        }

        if(progressBeanList.size() == 1){
            mProgress = progressBeanList.get(0).getProgress();
            textMeasure = mCenterTextPaint.measureText((int) mProgress + "%");
            canvas.drawText((int) mProgress + "%", (viewSize / 2) - textMeasure / 2, viewSize / 2 + progressTextSize / 2, mCenterTextPaint);
        }else {
            if(canDrawText){
                for (int i = 0; i < progressBeanList.size(); i++) {
                    rectF = new RectF(annulrWidth * ( (i*2+1) / 2), annulrWidth * ( (i*2+1) / 2), viewSize - annulrWidth * ( (i*2+1) / 2), viewSize - annulrWidth * ( (i*2+1) / 2));
                    Path path = new Path();
                    path.addArc(rectF , -90 ,progressBeanList.get(i).getProgress() /100 *360 );
                    canvas.drawTextOnPath(progressBeanList.get(i).getProgress()+"%" , path , 0,(annulrWidth+annulrTextSize)/2, mAnnulrTextPaint);
                }
            }
        }

    }

    private void setProgress(final int index) {
        synchronized (progresses) {
            if (progresses[index] > 100) {
                progresses[index] = progresses[index] % 100;
            }
            final float finalProgress = progresses[index];
            progresses[index] = 0;
            new Thread() {
                @Override
                public void run() {

                    while (progresses[index] <= finalProgress) {
                        try {
                            sleep(1);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        Message msg = Message.obtain();
                        progresses[index] += 0.1;
                        msg.obj = progresses[index];
                        handler.sendMessage(msg);
                    }
                    if(index == progressBeanList.size()-1){
                        canDrawText = true;
                    }
                }
            }.start();
        }
    }


    public void setProgress(ArrayList<ProgressBean> progressBeanList) {
        this.progressBeanList.clear();
        this.progressBeanList.addAll(progressBeanList);
        for (int i = 0; i < progressBeanList.size(); i++) {
            ProgressBean pb = progressBeanList.get(i);
            progresses[i] = pb.getProgress();
            colors[i] = pb.getCloor();
            setProgress(i);
        }
    }

}
